package arithmetic.guaveTest;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

/**
 * @author jiangfeng
 * Created on 2020-02-24
 */
public class TestCache {

    /**
     * 测试结果, key的失效时间是分开控制的,从写入时开始算 5分钟之后失效.
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        Cache<String, String> cache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.SECONDS).build();
        System.out.println("初始大小map:"+ cache.size());
        cache.put("token1","name1");
        Thread.sleep(1000);
        cache.put("token2","name2");
        Thread.sleep(1000);
        cache.put("token3","name3");

        Map<String,Cache<String,String>> maps = new HashMap<>();
        for (int i = 0; i <20 ; i++) {
            System.out.println(cache.size());
            System.out.println( cache.getIfPresent("token3"));
            Thread.sleep(1000L);
            System.out.println(cache.asMap());
        }
    }
}
